package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PublicReviewManage;
import com.ruoyi.system.mapper.CompeWorkScroingRecordMapper;
import com.ruoyi.system.mapper.PublicReviewManageMapper;
import com.ruoyi.system.service.PublicReviewManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 大众评审管理表(PublicReviewManage)表服务实现类
 *
 * @author makejava
 * @since 2021-08-27 14:57:03
 */
@Service("publicReviewManageService")
public class PublicReviewManageServiceImpl implements PublicReviewManageService {
    @Resource
    private PublicReviewManageMapper publicReviewManageMaper;
    @Resource
    private CompeWorkScroingRecordMapper compeWorkScroingRecordMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param mId 主键
     * @return 实例对象
     */
    @Override
    public PublicReviewManage queryById(Integer mId) {
        return this.publicReviewManageMaper.queryById(mId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<PublicReviewManage> queryAllByLimit(int offset, int limit) {
        return this.publicReviewManageMaper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param publicReviewManage 实例对象
     * @return 实例对象
     */
    @Transactional
    @Override
    public PublicReviewManage insert(PublicReviewManage publicReviewManage) throws Exception {
        if (StringUtils.isEmpty(publicReviewManage.getScoringIds())) {
            throw new Exception("请选择参加大众评审的作品");
        }
        //插入大众评审管理
        publicReviewManageMaper.insert(publicReviewManage);
        //插入参加大众评审的Scoring ID
        compeWorkScroingRecordMapper.updatePublicReview(publicReviewManage.getMId(), publicReviewManage.getScoringIds());
        return publicReviewManage;
    }

    /**
     * 修改数据
     *
     * @param publicReviewManage 实例对象
     * @return 实例对象
     */
    @Override
    public PublicReviewManage update(PublicReviewManage publicReviewManage) {
        this.publicReviewManageMaper.update(publicReviewManage);
        return this.queryById(publicReviewManage.getMId());
    }

    /**
     * 通过主键删除数据
     *
     * @param mId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer mId) {
        return this.publicReviewManageMaper.deleteById(mId) > 0;
    }


    @Override
    public boolean calculatePublicReviewScore() {
        return false;
    }
}
